<?php
/**
 * http://www.shoppingcartcore.com/
 * @property CartComponent $Cart
 * */
App::uses ( 'AppController', 'Controller' );
class ShopController extends AppController {
	public $components = array (
			'Cart',
			'Paypal' 
	);
	
	public $uses = array (
			'Item' 
	);
	
	public function beforeFilter() {
		parent::beforeFilter ();
		$this->Auth->allow ( 'itemupdate','updateshipping', "cart", "clear", "update", "step2", "review", "success", "carts" );
		$this->disableCache ();
		// $this->Security->validatePost = false;
	}
	
	public function clear() {
		$this->Cart->clear ();
		$this->Session->setFlash ( 'All item(s) removed from your shopping cart', 'flash_error' );
		$this->redirect ( '/shop/cart' );
	}
	
	public function add() {
		if ($this->request->is ( 'post' )) {
			$id = $this->request->data ['Item'] ['id'];
			$quantity = $this->request->data ['Item'] ['quantity'];
			$item = $this->Cart->add ( $id, $quantity );
		}
		if (! empty ( $item )) {
			$this->Session->setFlash ( $item ['Item'] ['name'] . ' was added to your shopping cart.', 'flash_success' );
		}
		$this->redirect ( $this->referer () );
	}
	
	public function carts() {
		header ( 'Content-type: application/json' );
		$cart = $this->Session->read ( 'Shop' );
		echo json_encode ( $cart );
		$this->autoRender = false;
		exit ();
	}
	public function itemupdate() {
		header ( 'Content-type: application/json' );
		if ($this->request->is ( 'ajax' )) {
			$this->Cart->add ( $this->request->data ['id'], $this->request->data ['quantity'] );
		}
		$cart = $this->Session->read ( 'Shop' );
		echo json_encode ( $cart );
		$this->autoRender = false;
		exit ();
	}
	
	public function update() {
		$this->Cart->update ( $this->request->data ['Item'] ['id'], 1 );
	}
	
	public function remove($id = null) {
		$item = $this->Cart->remove ( $id );
		if (! empty ( $item )) {
			$this->Session->setFlash ( $item ['Item'] ['name'] . ' was removed from your shopping cart', 'flash_error' );
		}
		$this->redirect ( array (
				'action' => 'cart' 
		) );
	}
	
	public function cartupdate() {
		if ($this->request->is ( 'post' )) {
			foreach ( $this->request->data ['Item'] as $key => $value ) {
				$p = explode ( '-', $key );
				$this->Cart->add ( $p [1], $value );
			}
			$this->Session->setFlash ( 'Shopping Cart is updated.', 'flash_success' );
		}
		$this->redirect ( array (
				'action' => 'cart' 
		) );
	}
	public function updateshipping() {
		if ($this->request->is ( 'post' )) {
			$val = empty ( $_REQUEST ["shop_fare"] ) ? 0 : intval ( $_REQUEST ["shop_fare"] );
			$ammount = $this->Cart->updateShipping ( $val);
			$this->autoRender = false;
			echo json_encode ( $ammount );
			exit ();
		}
		die();
	}
	
	public function cart() {
		$shop = $this->Session->read ( 'Shop' );
		$this->set ( compact ( 'shop' ) );
	}
	
	public function googlecheckout() {
		$this->helpers [] = 'Google';
		$shop = $this->Session->read ( 'Shop' );
		$this->set ( compact ( 'shop' ) );
	}
	
	public function address() {
		
		$shop = $this->Session->read ( 'Shop' );
		if (! $shop ['Order'] ['total']) {
			$this->redirect ( '/' );
		}
		
		if ($this->request->is ( 'post' )) {
			$this->loadModel ( 'Order' );
			$orders = $this->request->data;
			$orders ['Order'] ['billing_address'] = $orders ["Order"] ['shipping_address'];
			$orders ['Order'] ['billing_address2'] = $orders ["Order"] ['shipping_address2'];
			$orders ['Order'] ['billing_city'] = $orders ["Order"] ['shipping_city'];
			$orders ['Order'] ['billing_zip'] = $orders ["Order"] ['shipping_zip'];
			$orders ['Order'] ['billing_state'] = $orders ["Order"] ['shipping_state'];
			$orders ['Order'] ['billing_country'] = $orders ["Order"] ['shipping_country'];

			$this->Order->set ( $orders );
			if ($this->Order->validates ()) {
				$order = $orders ['Order'];
				$order ['order_type'] = 'creditcard';
				$this->Session->write ( 'Shop.Order', $order + $shop ['Order'] );
				$this->redirect ( array (
						'action' => 'review' 
				) );
			} else {
				$this->Session->setFlash ( 'The form could not be saved. Please, try again.', 'flash_error' );
			}
		}
		if (! empty ( $shop ['Order'] )) {
			$this->request->data ['Order'] = $shop ['Order'];
		}
	
	}
	
	public function step1() {
		$paymentAmount = $this->Session->read ( 'Shop.Order.total' );
		if (! $paymentAmount) {
			$this->redirect ( '/' );
		}
		$this->Session->write ( 'Shop.Order.order_type', 'paypal' );
		$cart = $this->Session->read ( 'Shop' );
		$this->Paypal->step1 ( $cart );
	}
	
	public function step2() {
		$token = $this->request->query ['token'];
		$paypal = $this->Paypal->GetShippingDetails ( $token );
//		http://localhost/CakePHP//shop/step2?token=EC-09H99129TD669825L&PayerID=7FH3FKA6PDNAJ

		$ack = strtoupper ( $paypal ["ACK"] );
		if ($ack == "SUCCESS" || $ack == "SUCESSWITHWARNING") {
			$this->Session->write ( 'Shop.Paypal.Details', $paypal );
			$this->redirect ( array (
					'action' => 'review' 
			) );
		} else {
			$ErrorCode = urldecode ( $paypal ["L_ERRORCODE0"] );
			$ErrorShortMsg = urldecode ( $paypal ["L_SHORTMESSAGE0"] );
			$ErrorLongMsg = urldecode ( $paypal ["L_LONGMESSAGE0"] );
			$ErrorSeverityCode = urldecode ( $paypal ["L_SEVERITYCODE0"] );
			echo "GetExpressCheckoutDetails API call failed. ";
			echo "Detailed Error Message: " . $ErrorLongMsg;
			echo "Short Error Message: " . $ErrorShortMsg;
			echo "Error Code: " . $ErrorCode;
			echo "Error Severity Code: " . $ErrorSeverityCode;
			die ();
		}
	
	}
	
	public function review() {
		
		$shop = $this->Session->read ( 'Shop' );

		if (empty ( $shop )) {
			$this->redirect ( '/' );
		}
			
		if ($this->request->is ( 'post' )) {
			
			$this->loadModel ( 'Order' );
			
			$this->Order->set ( $this->request->data );	
			
			if ($this->Order->validates ()) {
				$order = $shop;
				
				$order ['Order'] ['status'] = 1;
				$order ['Order'] ['ip_address'] = $this->request->clientIp ( false );
				$customerid = $this->Session->read('Auth.User.id');
                $order ['Order']['customer_id'] =$customerid;

				if ($shop ['Order'] ['order_type'] == 'paypal') {
					$paypal = $this->Paypal->ConfirmPayment ( $order ['Order'] ['total'] );
					$ack = strtoupper ( $paypal ['ACK'] );
					if ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
						$order ['Order'] ['status'] = 2;
					}
					$order ['Order'] ['authorization'] = $paypal ['ACK'];
					// $order['Order']['transaction'] =
					// $paypal['PAYMENTINFO_0_TRANSACTIONID'];
				}
				

				$save = $this->Order->saveAll ( $order, array (
						'validate' => 'first' 
				) );

				/*$this->sendToMe(json_encode($order),"order");*/
				if ($save) {

                    $item_ids=array();
                    foreach ($shop['OrderItem'] as $orderitem){
                        $item_ids[]=$orderitem['Item']['id'];
                    }

                    if(!empty($item_ids)){
                        $sql = "UPDATE " . $this->Item->useTable . " set item_status_id=2 where id in(" . implode ( ",", $item_ids ) . ")";
                        $this->Item->query ( $sql );
                    }
									

					$this->set ( compact ( 'shop' ) );

				/*	try {
						/*
						 * $this->Email->template = 'order'; $this->Email->from
						 * = 'mofing@163.com'; $this->Email->to = $shop
						 * ['Order'] ['email']; $this->Email->subject = 'Shop
						 * Order'; $this->Email->sendAs = 'both';
						 * $this->Email->delivery = 'smtp'; $this->set ( 'shop',
						 * $shop ); $this->Email->send ();
						 */

                      /*  $email_data = array(
                            "email"=>"philippa410@gmail.com",
                            "subject"=> 'Order Receipt',
                            "shop" => $shop
                        );
                        $this->sendEmailByTemplate ( $email_data, "order" );

                        $email_data = array (
                            "email" => $shop ['Order'] ['email'],
                            "subject" => 'Order Receipt',
                            "shop" => $shop,
                        );
                        $this->sendEmailByTemplate ( $email_data, "order" );


					} catch ( Exception $e ) {
						$this->set ( 'smtp_errors', $this->Email->smtpError );
					}*/

					App::uses('CakeEmail', 'Network/Email');
					$email = new CakeEmail();
					$email->from(array('noreply@'.env('SERVER_NAME').".com" =>  'My Site'));
					$email->to($order['Order']['email'],'philippa410@gmail.com');
					$email->subject('Order Receipt');
					$email->template('order');
					$email->emailFormat('html');
				/*	$email->viewVars(array(
					'activate_url' => $shop['OrderItem'],
					'username' => $customer['Customer']['username'])
					);*/
					$email->send();
					
					
					$this->redirect ( array (
							'action' => 'success' 
					) );
				} else {
					$errors = $this->Order->invalidFields ();
					$this->set ( compact ( 'errors' ) );
				}
			}
		}
		
		if (($shop ['Order'] ['order_type'] == 'paypal') && ! empty ( $shop ['Paypal'] ['Details'] )) {
			$shop ['Order'] ['first_name'] = $shop ['Paypal'] ['Details'] ['FIRSTNAME'];
			$shop ['Order'] ['last_name'] = $shop ['Paypal'] ['Details'] ['LASTNAME'];
			$shop ['Order'] ['email'] = $shop ['Paypal'] ['Details'] ['EMAIL'];
			$shop ['Order'] ['phone'] = '';
			$shop ['Order'] ['billing_address'] = $shop ['Paypal'] ['Details'] ['SHIPTOSTREET'];
			$shop ['Order'] ['billing_address2'] = '';
			$shop ['Order'] ['billing_city'] = $shop ['Paypal'] ['Details'] ['SHIPTOCITY'];
			$shop ['Order'] ['billing_zip'] = $shop ['Paypal'] ['Details'] ['SHIPTOZIP'];
			$shop ['Order'] ['billing_state'] = $shop ['Paypal'] ['Details'] ['SHIPTOSTATE'];
			$shop ['Order'] ['billing_country'] = $shop ['Paypal'] ['Details'] ['SHIPTOCOUNTRYNAME'];
			
			$shop ['Order'] ['shipping_address'] = $shop ['Paypal'] ['Details'] ['SHIPTOSTREET'];
			$shop ['Order'] ['shipping_address2'] = '';
			$shop ['Order'] ['shipping_city'] = $shop ['Paypal'] ['Details'] ['SHIPTOCITY'];
			$shop ['Order'] ['shipping_zip'] = $shop ['Paypal'] ['Details'] ['SHIPTOZIP'];
			$shop ['Order'] ['shipping_state'] = $shop ['Paypal'] ['Details'] ['SHIPTOSTATE'];
			$shop ['Order'] ['shipping_country'] = $shop ['Paypal'] ['Details'] ['SHIPTOCOUNTRYNAME'];
			
			$shop ['Order'] ['order_type'] = 'paypal';
			
			$this->Session->write ( 'Shop.Order', $shop ['Order'] );
		}
		
		$this->set ( compact ( 'shop' ) );
	
	}
	
	public function success() {
		$shop = $this->Session->read ( 'Shop' );
		$this->Cart->clear ();
		if (empty ( $shop )) {
			$this->redirect ( '/' );
		}
		$this->set ( compact ( 'shop' ) );
	}

    public function order() {
        $this->loadModel ( 'Order' );
        $orders = $this->request->data;
    }


}
